//
//  KRPostInfoCell.m
//  cookBook
//
//  Created by kuroken on 16/1/16.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#import "KRPostInfoCell.h"
#import <UIButton+WebCache.h>
#import <PureLayout.h>
#import "KRConst.h"

// cell的标识
static NSString * const reuseIdentifier = @"postInfo";
// 头像的大小
static NSInteger const avatarImageWH = 30;

@interface KRPostInfoCell ()
@property (nonatomic, weak) UIButton *avatarBtn;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, weak) UILabel *viewCountLabel;
@end

@implementation KRPostInfoCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    KRPostInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[KRPostInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
//        self.backgroundColor = KRRandomColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self setupSubview];
        [self addConstraint];
    }
    return self;
}

- (void)setupSubview
{
    UIButton *avatarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:avatarBtn];
    self.avatarBtn =avatarBtn;
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.font = [UIFont systemFontOfSize:11];
    timeLabel.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    UILabel *viewCountLabel = [[UILabel alloc] init];
    viewCountLabel.font = [UIFont systemFontOfSize:12];
    viewCountLabel.textColor = KRColor(111, 143, 18);
    [self.contentView addSubview:viewCountLabel];
    self.viewCountLabel = viewCountLabel;
    
}

- (void)addConstraint
{
    [self.avatarBtn autoSetDimensionsToSize:CGSizeMake(avatarImageWH, avatarImageWH)];
    [self.avatarBtn autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:15];
    [self.avatarBtn autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:15];
    self.avatarBtn.layer.cornerRadius = avatarImageWH * 0.5;
    self.avatarBtn.clipsToBounds = YES;

    [self.nameLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.avatarBtn withOffset:-2];
    [self.nameLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.avatarBtn withOffset:10];
    
    [self.timeLabel autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.avatarBtn withOffset:2];
    [self.timeLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.nameLabel];
    
    [self.viewCountLabel autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:15];
    [self.viewCountLabel autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.avatarBtn withOffset:2];
    
}

- (void)setAvatar:(NSString *)avatar
{
    _avatar = avatar;
    
    [self.avatarBtn sd_setImageWithURL:[NSURL URLWithString:avatar] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"headico"]];

}

- (void)setUserName:(NSString *)userName
{
    _userName = userName;
    
    [self.nameLabel setText:userName];
}

- (void)setCreateTime:(NSString *)createTime
{
    _createTime = createTime;
    
    [self.timeLabel setText:[NSString stringWithFormat:@"发表于 %@", createTime]];
}

- (void)setViewCount:(NSString *)viewCount
{
    _viewCount = viewCount;
    
    [self.viewCountLabel setText:viewCount];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
